package raceReservationServer.raceServerGateway;

import java.rmi.RemoteException;
import java.util.LinkedList;
import java.util.List;

import raceReservationServer.raceServerGateway.proxies.*;

public class WSRaceServerGateway implements IRaceServerGateway {
	
	private ArabaRacesWSStub stub;
	public WSRaceServerGateway(String puri){
		System.out.println("Creating the Stubkhkhkh");
		String[] params= puri.split(" ");
		String uri= "http://" + params[0] + ":" + params[1] + "/axis2/services/" + params[2];
		System.out.println("Creating the Stub");
		try {
			
			stub= new ArabaRacesWSStub(uri);
			System.out.println("The service uri: " + uri);
		} catch (Exception e) {
			System.out.println("Error creating stub");
		}
	}
	
	public List<RaceDO> getRaces() {
		
		List<RaceDO> result = new LinkedList<RaceDO>();
		ArabaRacesWSStub.RaceDTO[] aux= null;
		
		System.out.println("Testing the stub..." + stub.toString());
		
		
		try {
			aux = stub.getRaces();
			
			String name= "";
			float dist= 0;
			String date= "";
			int places= 0;
			
			for(int i=0;i<aux.length;i++){
				name= aux[i].getName();
				dist= aux[i].getDistance();
				date= aux[i].getDate();
				places= aux[i].getAAvailablePlaces();
				result.add(new RaceDO(name, dist, date, places));
			}
		} catch (RemoteException e) {
			System.out.println("Error receiving the races from Web Service");
		}
		
	
		
		return result;
	}
}
